﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace d1
{
    internal class Program
    {
        static bool isempty(OrderService my)
        {
            foreach (order m in my.mylist)
                return true;
            return false;
        }
        static void Main(string[] args)
        {
            OrderService myorders = new OrderService();
            bool flag = true;
            while (flag)
            {
                Console.WriteLine("输入1添加订单，输入2删除订单，输入3查询订单，输入4修改订单，输入5查看所有订单，输入6退出");
                string input = Console.ReadLine();
                switch (input)
                {
                    case "1":
                        myorders.addOrder();
                        break;
                    case "2":
                        myorders.removeOrder();
                        break;
                    case "3":
                        Console.WriteLine("输入1按订单号查询，输入2按客户查询，输入3按金额查询,输入4按商品名称查询");
                        int i = Convert.ToInt32(Console.ReadLine());
                        myorders.searchOrder(i);
                        break;
                    case "4":
                        Console.WriteLine("请输入你需要修改的订单号");
                        myorders.reOrder();
                        break;
                     
                    case "5":
                        if (isempty(myorders) == false)
                            Console.WriteLine("现在无订单");
                        else
                        {
                            Console.WriteLine("以下是全部订单");
                            foreach (order my in myorders.mylist)
                            {
                                Console.WriteLine(my.ToString());
                                Console.WriteLine("此订单明细为：");
                                foreach (OrderDetails myd in my.orderDetails)
                                {

                                   
                                        Console.WriteLine(myd.ToString());
                                    
                                }
                            }
                        }
                        break;
                    case "6":
                        flag = false;
                        break;
                    default:
                        Console.WriteLine("错误");
                        break;
                }

            }
            myorders.mylist.Sort((p1, p2) => { return p1.Id - p2.Id; });//订单排序
        }
        public class OrderDetails
        {
            public string name;
            public int number;
            public double price;
            public override string ToString()
            {
                return "商品名称：" + name + "  商品单价：" + price + "  商品数量：" + number;
            }
            public OrderDetails()
            {
                name = string.Empty;
                number = 0;
                price = 0;
            }

            public OrderDetails(string name, int number, double price)
            {
                this.name = name;
                this.number = number;
                this.price = price;
            }

            public override bool Equals(object obj)
            {
                return obj is OrderDetails details &&
                       name == details.name;
            }

            public double getAllPrice()
            {
                return this.price * this.number;
            }

            public override int GetHashCode()
            {
                return 363513814 + EqualityComparer<string>.Default.GetHashCode(name);
            }
        }
        public class order : IComparable
        {
            public int Id;
            public string Customer;
            public double sumMoney;
            public int date;
            public override string ToString()
            {
                return "订单号：" + Id + "  客户：" + Customer + "  金额：" + sumMoney+"  日期为："+date;
            }

            public List<OrderDetails> orderDetails = new List<OrderDetails>();


            public order()
            {
                this.Id = 0;
                this.Customer = string.Empty;
                this.sumMoney = 0;
                this.date = 0;
            }
          
            public order(int id, string customer,int date)
            {
                this.Id = id;
                this.Customer = customer;
                this.sumMoney = 0;
                this.date = date;
            }

            public void setMoney()
            {
                double mymoney = 0;
                foreach (OrderDetails details in orderDetails)
                {
                    mymoney += details.getAllPrice();
                }
                this.sumMoney = mymoney;
            }
        public bool isSame(string name){
               
              
                var query = from s in orderDetails
                            where s.name == name
                            select s;
                foreach (OrderDetails my in query)
                    return true;
                return false;
            }
    public void addOrderDetails(string name, int number, double price)
            {
                OrderDetails details = new OrderDetails(name, number, price);
                this.orderDetails.Add(details);
            }
            public int CompareTo(object obj2)
            {
                if (!(obj2 is order))
                    throw new System.ArgumentException();
                order p2 = (order)obj2;
                return this.Id - p2.Id;
            }

            public override bool Equals(object obj)
            {
                return obj is order order &&
                       Id == order.Id;
            }

            public override int GetHashCode()
            {
                return 2108858624 + Id.GetHashCode();
            }
        }
        public class OrderService
        {
            public List<order> mylist = new List<order>();

            public OrderService() { }



            public void addOrder()
            {
                try
                {
                    int chance = 0;
                    Console.WriteLine("请输入订单号:");
                    int id = Convert.ToInt32(Console.ReadLine());
                    foreach(order myo in mylist)
                    {
                        if (myo.Id == id)
                            chance++;
                    }
                    if (chance != 0)
                        Console.WriteLine("有重复订单");
                    else
                    {
                        Console.WriteLine("请输入订单客户:");
                        string customer = Console.ReadLine();
                        Console.WriteLine("请输入日期（例如2022年6月15日请输入20220615）:");
                        int date = Convert.ToInt32(Console.ReadLine());
                        order my = new order(id, customer,date);

                        bool judge = true;
                        bool same = false;
                        foreach (order o in mylist)
                        {
                            if (o.Equals(my))
                                same = true;
                        }
                        if (same)
                        {
                            Console.WriteLine("有重复订单号");
                        }
                        else
                        {
                            Console.WriteLine("添加订单明细");
                            while (judge && !same)
                            {
                                Console.WriteLine("请输入订单商品名称:");
                                string name = Console.ReadLine();
                                Console.WriteLine("请输入此商品数量:");
                                int number = Convert.ToInt32(Console.ReadLine());
                                Console.WriteLine("请输入此商品的价格:");
                                double price = Convert.ToDouble(Console.ReadLine());
                                my.addOrderDetails(name, number, price);
                                Console.WriteLine("继续添加商品么？（如果继续请输入yes,输入其他默认不继续添加商品)");
                                string x = Console.ReadLine();
                                if (x == "yes")
                                    judge = true;
                                else
                                    judge = false;
                            }
                            mylist.Add(my);
                            my.setMoney();
                            Console.WriteLine("成功加入了一笔订单");
                        }
                    }
                }
                catch
                {
                    Console.WriteLine("错误");
                }
            }

            public void removeOrder()
            {
                try
                {
                    Console.WriteLine("请输入订单号");
                    int id = Convert.ToInt32(Console.ReadLine());
                    int index = 0;
                    int chance = 0;
                    foreach (order my in mylist)
                    {
                        if (my.Id == id)
                        {
                            chance++;
                            index = this.mylist.IndexOf(my);
                        }
                    }
                    if (chance > 0)
                    {
                        this.mylist.RemoveAt(index);
                        Console.WriteLine("成功删除");
                    }
                    else
                        Console.WriteLine("未找到");
                }
                catch
                {
                    Console.WriteLine("错误");
                }
            }
            public void reOrder()
            {
                try
                {

                    int id = Convert.ToInt32(Console.ReadLine());
                    int index = 0;
                    int chance = 0;
                    foreach (order my in mylist)
                    {
                        if (my.Id == id)
                        {
                            chance++;
                            Console.WriteLine("已找到");
                            Console.WriteLine("输入1修改金额，输入2修改客户名称，输入3修改日期，输入4修改订单明细");
                            int i = Convert.ToInt32(Console.ReadLine());
                            switch (i)
                            {
                                case 1:
                                    Console.WriteLine("请输入新的金额");
                                    int money = Convert.ToInt32(Console.ReadLine());
                                    my.sumMoney = money;
                                    Console.WriteLine("修改完成");

                                    break;
                                case 2:
                                    Console.WriteLine("请输入新的客户名字");
                                    string customer = Console.ReadLine();
                                    my.Customer = customer;
                                    Console.WriteLine("修改完成");
                                    break;
                                case 3:
                                    Console.WriteLine("请输入新日期(例如2020年6月15日请输入20200615)：");
                                    int date = Convert.ToInt32(Console.ReadLine());
                                    my.date = date;
                                    Console.WriteLine("修改完成");
                                    break;
                                case 4:
                                    Console.WriteLine("请输入要修改的商品名称");
                                    string goods = Console.ReadLine();
                                    foreach (OrderDetails g in my.orderDetails)
                                    {
                                        if (g.name == goods)
                                        {
                                            Console.WriteLine("输入1修改单价，输入2修改数量");
                                            int im = Convert.ToInt32(Console.ReadLine());
                                            switch (im)
                                            {
                                                case 1:
                                                    Console.WriteLine("请输入新的单价");
                                                    int price = Convert.ToInt32(Console.ReadLine());
                                                   g.price = price;
                                                    my.setMoney();
                                                    Console.WriteLine("修改成功");
                                                    break;
                                                case 2:
                                                    Console.WriteLine("请输入新的数量");
                                                    int num = Convert.ToInt32(Console.ReadLine());
                                                    g.number =num;
                                                    my.setMoney();
                                                    break;
                                                default:
                                                    Console.WriteLine("错误");
                                                    break;
                                            }
                                            }
                                    }
                                    Console.WriteLine("修改成功");
                                    break;

                                default:
                                    Console.WriteLine("错误");
                                    this.mylist.RemoveAt(index);
                                    break;
                            }

                        }
                        
                    }

                    if (chance == 0)
                        Console.WriteLine("未找到");



                }
                catch
                {
                    Console.WriteLine("错误");
                }
            }
            public void searchOrder(int i)
            {
                int chance = 0;
                try
                {
                    switch (i)
                    {
                        case 1:

                         
                                Console.WriteLine("请输入订单号");
                                int id = Convert.ToInt32(Console.ReadLine());
                                var query = from s in mylist
                                            where s.Id == id
                                            orderby s.Id
                                            select s;
                                foreach (order m in query)
                                {
                                chance++;
                                    Console.WriteLine(m.ToString());
                                    Console.WriteLine("此订单明细为：");
                                    foreach (OrderDetails d in m.orderDetails)
                                    {
                                        Console.WriteLine(d.ToString());
                                    }
                                }
                            if (chance == 0)
                                Console.WriteLine("未找到");
                            break;
                        case 2:
                            chance = 0;
                            Console.WriteLine("请输入客户名字");
                            string customer = Console.ReadLine();
                            var query1 = from s in mylist
                                        where s.Customer == customer
                                        orderby s.Id
                                        select s;
                            foreach (order my in query1)
                            {
                                chance++;
                                    Console.WriteLine(my.ToString());
                                Console.WriteLine("此订单明细为：");
                                foreach (OrderDetails d in my.orderDetails)
                                {
                                    Console.WriteLine(d.ToString());
                                }
                            }
                            if (chance == 0)
                                Console.WriteLine("未找到");
                            break;
                        case 3:
                            chance = 0;
                            Console.WriteLine("请输入金额");
                            int mymoney = Convert.ToInt32(Console.ReadLine());
                            var query2 = from s in mylist
                                         where s.sumMoney == mymoney
                                         orderby s.Id
                                         select s;
                            foreach (order my in query2)
                            {
                                chance++;
                                    Console.WriteLine(my.ToString());
                                Console.WriteLine("此订单明细为：");
                                foreach(OrderDetails d in my.orderDetails)
                                {
                                    Console.WriteLine(d.ToString());
                                }
                            }
                              if (chance == 0)
                                Console.WriteLine("未找到");
                            break;
                        case 4:
                            chance = 0;
                            Console.WriteLine("商品名称");
                            string gname = Console.ReadLine();
                            var query3 = from s in mylist
                                         where s.isSame(gname)==true
                                         orderby s.sumMoney
                                         select s;
                            foreach (order MY in query3)
                            {
                                chance++;
                                        Console.WriteLine(MY.ToString());
                                        Console.WriteLine("此订单明细为：");
                                foreach (OrderDetails d in MY.orderDetails)
                                {
                                    Console.WriteLine(d.ToString());
                                }



                            }
                            if (chance == 0)
                                Console.WriteLine("未找到");
                            break;
                        default:
                            Console.WriteLine("未找到");
                            break;
                    }
                }

                catch
                {
                    Console.WriteLine("错误");
                }
            }



        }
    }
}
